﻿
@{
    ViewBag.title = "index";
    Layout = "/Views/Shared/_Layout_iframe.cshtml";
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Add</title>
    <style>
        form {
            margin: 30px auto;
            width: 80%;
            max-width: 800px;
        }

        .layui-form-item {
            margin-bottom: 15px;
        }
    </style>

</head>
<body>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>商品添加页面</legend>
    </fieldset>
    <form class="layui-form" lay-filter="roleform">
        <div class="layui-form-item">
            <label class="layui-form-label">商品名称</label>
            <div class="layui-input-inline">
                <input type="text" name="ProductName" id="searchName" class="layui-input" lay-verify="required" placeholder="请输入商品名称" style="width:400px;" />
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">商品数量</label>
                <div class="layui-input-inline">
                    <input type="text" name="Number" lay-verify="required" placeholder="商品数量" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline" id="inventoryDiv" style="display: none;">
                <label class="layui-form-label">库存数量</label>
                <div class="layui-input-inline">
                    <input type="text" name="InboundNumber" id="productNumber" placeholder="库存数量" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">商品厂家</label>
                <div class="layui-input-inline">
                    <input type="text" name="ProductFactory" id="Factory" lay-verify="required" placeholder="商品厂家" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">采购商</label>
                <div class="layui-input-inline">
                    <input type="text" name="PurcharName" lay-verify="required" placeholder="采购商" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">采购价格</label>
                <div class="layui-input-inline">
                    <input type="text" name="PurcharPrice" lay-verify="required" placeholder="采购价格" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">零售价</label>
                <div class="layui-input-inline">
                    <input type="text" name="Price" id="productPrice" lay-verify="required" placeholder="零售价" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">阈值</label>
            <div class="layui-input-inline">
                <input type="text" name="Threshold" id="threshold" class="layui-input" lay-verify="required" placeholder="请输入阈值" autocomplete="off"  />
            </div>

        </div>
        <input type="hidden" name="Id" id="hfid" />
        <button type="submit" class="layui-btn" lay-submit lay-filter="demo1" style="margin-left:40px;">入库</button>
    </form>
    <script>
        let productId=$("#hfid").val();
        layui.use(['form', 'util', 'laydate','autocomplete'], function () {
            var form = layui.form;
            var layer = layui.layer;
            var util = layui.util;
            var autocomplete = layui.autocomplete;
            //var layedit = layui.layedit;

            autocomplete.render({
                elem: $('#searchName'),
                cache: false,
                url: '/Product/SearchName?name=' + $("#searchName").val(),
                template_val: '{{d.Id}}',       // 直接访问数组元素属性
                template_txt: '{{d.ProductName}}',
                done: function (res) {
                    console.log("数据加载成功：", res.data);
                },
                onselect: function (res) {
                    console.log("选中：", res);
                    $("#searchName").val(res.ProductName);
                    $("#hfid").val(res.Id);
                    $("#productNumber").val(res.Number); 
                    $("#Factory").val(res.ProductFactory);
                    $("#productPrice").val(res.Price);
                    $("#threshold").val(res.Threshold);
                    productId =res.Id;
                    console.log(productId);
                    //库存数量显现
                    document.getElementById('inventoryDiv').style.display = 'inline-block';
                    setTimeout(function () {
                        $('.layui-form-autocomplete').hide();
                    }, 100);
                }
            });
            console.log(productId);
            //提交事件
            form.on('submit(demo1)', function (data) {
                if (productId > 0) {
                  
                
                    $.post("/product/InboundInsert", { productData: encodeURIComponent(JSON.stringify(data.field)), productId:productId }, function (resp) {
                       
                        if (resp.code == 0) {
                            layer.msg('入库成功', function () {
                                // 父页面的table重载
                                //parent.layui.table.reload('testProduct');
                                // 关闭弹出层
                                var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
                                parent.layer.close(index);
                            });
                        }
                    });
                } else {
                    data.field.Id = 0
                    console.log(data.field)
                    $.post("/product/ProductInsert", { productData: encodeURIComponent(JSON.stringify(data.field)) }, function (resp) {
                        if (resp.code == 0) {
                            layer.msg('添加成功', { time: 1000 }, function () {
                                // 父页面的table重载
                                //parent.layui.table.reload('testProduct');
                                // 关闭弹出层
                                var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
                                parent.layer.close(index);
                            });
                        }
                    });
                }
                return false;
            });
        });
    </script>
</body>
</html>
